package com.biostime.dingdingbox.modules.db.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.biostime.dingdingbox.common.config.Global;
import com.biostime.dingdingbox.common.persistence.Page;
import com.biostime.dingdingbox.common.utils.StringUtils;
import com.biostime.dingdingbox.common.web.BaseController;
import com.biostime.dingdingbox.modules.db.entity.Album;
import com.biostime.dingdingbox.modules.db.entity.Box;
import com.biostime.dingdingbox.modules.db.entity.GameStrategy;
import com.biostime.dingdingbox.modules.db.service.AlbumService;
import com.biostime.dingdingbox.modules.db.service.BoxService;
import com.biostime.dingdingbox.modules.db.service.GameStrategyService;

/**
 * 玩法攻略表Controller
 * @author chenweicheng
 * @version 2016-06-29
 */
@Controller
@RequestMapping(value = "${adminPath}/db/gameStrategy")
public class GameStrategyController extends BaseController {

	@Autowired
	private GameStrategyService gameStrategyService;
	
	@Autowired
	private BoxService boxService;
	
	@Autowired
	private AlbumService albumService;
	
	@ModelAttribute
	public GameStrategy get(@RequestParam(required=false) String id) {
		GameStrategy entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = gameStrategyService.get(id);
		}
		if (entity == null){
			entity = new GameStrategy();
		}
		return entity;
	}
	
	@RequiresPermissions("db:gameStrategy:view")
	@RequestMapping(value = {"list", ""})
	public String list(GameStrategy gameStrategy, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<GameStrategy> page = gameStrategyService.findPage(new Page<GameStrategy>(request, response), gameStrategy); 
		model.addAttribute("page", page);
		return "modules/db/gameStrategyList";
	}

	@RequiresPermissions("db:gameStrategy:view")
	@RequestMapping(value = "form")
	public String form(GameStrategy gameStrategy, Model model) {
		List<Box>  boxList = boxService.findList(new Box());		
		model.addAttribute("boxList", boxList);		
		model.addAttribute("gameStrategy", gameStrategy);
		
		if(gameStrategy.getId() != null && gameStrategy.getId() !=""){
			Album ca = new Album();
			ca.setCategory("gameStrategy");
			ca.setObjectId(StringUtils.toInteger(gameStrategy.getId()));
			List<Album> gameStrategyAlbum = albumService.findList(ca);
			model.addAttribute("gameStrategyAlbum", gameStrategyAlbum);
		}				
		
		return "modules/db/gameStrategyForm";
	}

	@RequiresPermissions("db:gameStrategy:edit")
	@RequestMapping(value = "save")
	public String save(GameStrategy gameStrategy, Model model, RedirectAttributes redirectAttributes,HttpServletRequest request) {
		if (!beanValidator(model, gameStrategy)){
			return form(gameStrategy, model);
		}
		//try{
			gameStrategyService.saveGameStrategyByWeb(gameStrategy, request); 
/*			addMessage(redirectAttributes, "保存成功");
		}catch(Exception e){
			addMessage(redirectAttributes, "保存失败"+e.getMessage()); 
		}		*/
		return "redirect:"+Global.getAdminPath()+"/db/gameStrategy/?repage";
	}
	
	@RequiresPermissions("db:gameStrategy:edit")
	@RequestMapping(value = "delete")
	public String delete(@RequestParam(value="ids",required=false) String idsRequest, GameStrategy gameStrategy, RedirectAttributes redirectAttributes) {
		//批量删除
		if(!StringUtils.isEmpty(idsRequest)){
			String[] ids = idsRequest.split(",");
			for(String id : ids){
				gameStrategyService.delete(new GameStrategy(id));
			}
		}else{
			gameStrategyService.delete(gameStrategy);
		}
		
		addMessage(redirectAttributes, "删除成功");
		return "redirect:"+Global.getAdminPath()+"/db/gameStrategy/?repage";
	}

}